package lk.mrt.cse.wihidum.listener.processor;

import org.apache.axiom.om.*;
import org.apache.log4j.*;
import org.apache.axiom.soap.*;

import javax.xml.namespace.QName;
import lk.mrt.cse.wihidum.util.*;
import lk.mrt.cse.wihidum.listener.storage.*;
/**
 * Created by IntelliJ IDEA.
 * User: Will
 * Date: Feb 14, 2010
 * Time: 11:02:40 PM
 * To change this template use File | Settings | File Templates.
 */
public class SinkSubscriptionProcessor
{
    private static final Logger logger = Logger.getLogger (SinkSubscriptionProcessor.class);
   
    public void processSubscriptionResponseMessage(SOAPEnvelope response)
    {
             logger.info("Processing Subscription Response Message");

             SOAPHeader responseHeader=response.getHeader();
             SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
             OMNamespace wihidumNS = factory.createOMNamespace(WSEventingConstants.WIHIDUM_NAMESPACE, WSEventingConstants.WHIDUM_NAMESPACE_PREFIX);
             OMElement mySubscriptionElement=responseHeader.getFirstChildWithName(new QName(WSEventingConstants.WIHIDUM_NAMESPACE,WSEventingConstants.ElementNames.SubscriptionID));
             String subscriptionID=mySubscriptionElement.getText();

             OMElement filterElement=responseHeader.getFirstChildWithName(new QName(WSEventingConstants.WIHIDUM_NAMESPACE,WSEventingConstants.ElementNames.Filter));
             String filter=filterElement.getText();

             SOAPBody responseBody=response.getBody();
             OMElement responseElement=responseBody.getFirstChildWithName(new QName(WSEventingConstants.EVENTING_NAMESPACE,WSEventingConstants.ElementNames.SubscribeResponse));
             OMElement subManagerElement=responseElement.getFirstChildWithName(new QName(WSEventingConstants.EVENTING_NAMESPACE,WSEventingConstants.ElementNames.SubscriptionManager));
             OMElement subManagerAddressElement=subManagerElement.getFirstChildWithName(new QName(WSEventingConstants.ADDRESSING_NAMESPACE,WSEventingConstants.ElementNames.Address));
             String subManagerAddress=subManagerAddressElement.getText();

             OMElement referenceParametersElement=subManagerElement.getFirstChildWithName(new QName(WSEventingConstants.ADDRESSING_NAMESPACE,WSEventingConstants.ElementNames.ReferenceParameters));
             OMElement identifierElement=referenceParametersElement.getFirstChildWithName(new QName(WSEventingConstants.EVENTING_NAMESPACE,WSEventingConstants.ElementNames.Identifier));
             String subscriberUUID=identifierElement.getText();

             Subscription subscription=new Subscription();
             subscription.setUuid(subscriberUUID);
             subscription.setSubscriptionID(subscriptionID);
             subscription.setFilter(filter);
             subscription.setSubscriptionManagerAdress(subManagerAddress);

             logger.info("Saving the subscription details received in the subscription response.");

             SubscriptionDAO subscriptionDAO=new SubscriptionDAO();
             subscriptionDAO.saveSubscriber(subscription);
    }
}
